﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBlock;using Vector3Int = VoxelBlock.Vector3Int;

public class MoveBrushComponent : MonoBehaviour
{

    /// <summary>
    /// UI层级
    /// </summary>
    [SerializeField]
    [HeaderAttribute("UI层级"), Space(-5)]
    LayerMask uiLayerMask;

    /// <summary>
    /// 打在UI上的相机
    /// </summary>
    [SerializeField]
    [HeaderAttribute("打在UI上的相机"), Space(-5)]
    Camera uiCamera;


    [SerializeField]
    Camera selectCamera;

    [SerializeField]
    LayerMask transformLayerMask;

    [SerializeField]
    LayerMask layerMask;
    [SerializeField]
    public Transform selectBrushTransform;

    [SerializeField]
    Transform directionMark;

    VoxelEditorMastermind mastermind;


    void Start()
    {
        mastermind = VoxelEditorMastermind.instance;

    }

    //RaycastHit objhit;
    const float eposelong = 0.01f;

    void Update()
    {
       
    }

    Vector3 pointDown;
    Vector3 pointNow;

    public bool UpdateBrush()
    {
        Vector3 cursorPos = Input.mousePosition;
        Ray uiCameraRay = uiCamera.ScreenPointToRay(cursorPos);
        RaycastHit objhit;
        if (Physics.Raycast(uiCameraRay, out objhit, 100, uiLayerMask))
        {
            ConfigCamera.isUI = true;
            return false;
        }
        else
        {
            ConfigCamera.isUI = false;
        }
        Ray ray = selectCamera.ScreenPointToRay(cursorPos);
#if UNITY_EDITOR
        Debug.DrawLine(ray.origin, objhit.point, Color.red, 0.1f);
#endif
        if (VoxelEditorMastermind.instance && VoxelEditorMastermind.instance.Behavior == VoxelCategories.OperationBehavior.Transform)
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (Physics.Raycast(ray, out objhit, 10000, transformLayerMask))
                {
                    TransformVectorUnit vectorUnit;
                    vectorUnit = objhit.collider.gameObject.GetComponent<TransformVectorUnit>();
                    if (vectorUnit)
                    {
                        vectorUnit.Select();
                    }
                    else
                    {
                        TransformScaleBoxUnit scaleBoxUnit;
                        scaleBoxUnit = objhit.collider.gameObject.GetComponent<TransformScaleBoxUnit>();
                        if (scaleBoxUnit)
                        {
                            scaleBoxUnit.Select();
                        }
                    }

                    pointDown = cursorPos;
                    pointNow = cursorPos;
                }
                else
                {
                    if (Physics.Raycast(ray, out objhit, 10000, layerMask))
                    {
                        TransformUnitComponent tempTransformUnitComponent = objhit.collider.transform.root.gameObject.GetComponent<TransformUnitComponent>();
                        if (tempTransformUnitComponent)
                        {
                            TransformVector.Thas.Look = tempTransformUnitComponent.transform;
                            //UIItemTransformUnitComponent.UnLookThis();
                        }
                        else
                        {
                            if (TransformScaleBox.selectUnit)
                            {
                                TransformScaleBox.selectUnit.NoSelect();
                            }
                            if (TransformVector.selectUnit)
                            {
                                TransformVector.selectUnit.NoSelect();
                            }
                            UIItemTransformUnitComponent.UnLookThis();
                            TransformVector.Thas.gameObject.SetActive(false);
                        }
                    }
                    else
                    {
                        if (TransformScaleBox.selectUnit)
                        {
                            TransformScaleBox.selectUnit.NoSelect();
                        }
                        if (TransformVector.selectUnit)
                        {
                            TransformVector.selectUnit.NoSelect();
                        }
                        UIItemTransformUnitComponent.UnLookThis();
                        TransformVector.Thas.gameObject.SetActive(false);
                    }
                }
            }

            if (TransformVector.selectUnit)
            {
                if (Input.GetMouseButton(0))
                {
                    TransformVector.selectUnit.Move(cursorPos - pointDown);
                }
            }
            if (TransformScaleBox.selectUnit)
            {
                if (Input.GetMouseButton(0))
                {
                    TransformScaleBox.selectUnit.Move(cursorPos - pointDown);
                    pointNow = cursorPos;
                }
            }
            return false;
        }


        if (Physics.Raycast(ray, out objhit, 10000, layerMask))
        {
            Vector3 point = objhit.point;
            //是否内部
            bool inside = true;
            switch (mastermind.Behavior)
            {
                case VoxelCategories.OperationBehavior.Add:
                    inside = false;
                    break;
                case VoxelCategories.OperationBehavior.Remove:
                    inside = true;
                    break;
                case VoxelCategories.OperationBehavior.Paint:
                    //if (mastermind.IsDrawOver)
                    //{
                    //    inside = false;
                    //}
                    //else
                    {
                        inside = true;
                    }
                    break;
                case VoxelCategories.OperationBehavior.Read:
                    inside = true;
                    break;
                default:
                    break;
            }

            //float negativeAppendX = 0;
            //float negativeAppendY = 0;
            //float negativeAppendZ = 0;
            //if (point.x <= 0) { negativeAppendX = -1; }
            //if (point.y <= 0) { negativeAppendY = -1; }
            //if (point.z <= 0) { negativeAppendZ = -1; }

            selectBrushTransform.transform.position = new Vector3((int)point.x, (int)point.y, (int)point.z);
#if UNITY_EDITOR
            selectBrushTransform.name = point.x + " " + point.y + " " + point.z + " ";
#endif
            if (Mathf.Round(point.z * 10) == (int)point.z * 10)
            {
                if (inside)
                {
                    if (ray.direction.z > 0)
                    {
                        mastermind.brushDirection = VoxelDirection.Break;
                    }
                    else
                    {
                        mastermind.brushDirection = VoxelDirection.Front;
                    }
                }
                else
                {
                    if (ray.direction.z > 0)
                    {
                        mastermind.brushDirection = VoxelDirection.Break;
                    }
                    else
                    {
                        mastermind.brushDirection = VoxelDirection.Front;
                    }
                }
            }
            else if (Mathf.Round(point.x * 10) == (int)point.x * 10)
            {
                if (inside)
                {
                    if (ray.direction.x > 0)
                    {
                        mastermind.brushDirection = VoxelDirection.Left;
                    }
                    else
                    {
                        mastermind.brushDirection = VoxelDirection.Right;
                    }
                }
                else
                {
                    if (ray.direction.x > 0)
                    {
                        mastermind.brushDirection = VoxelDirection.Left;
                    }
                    else
                    {
                        mastermind.brushDirection = VoxelDirection.Right;
                    }
                }
            }
            else if (Mathf.Round(point.y * 10) == (int)point.y * 10)
            {
                if (inside)
                {
                    if (ray.direction.y > 0)
                    {
                        mastermind.brushDirection = VoxelDirection.Down;
                    }
                    else
                    {
                        mastermind.brushDirection = VoxelDirection.Up;
                    }
                }
                else
                {
                    if (ray.direction.y > 0)
                    {
                        mastermind.brushDirection = VoxelDirection.Down;
                    }
                    else
                    {
                        mastermind.brushDirection = VoxelDirection.Up;
                    }
                }
            }

            Vector3 pos = point -= new Vector3(0.5f, 0.5f, 0.5f);
            if (inside)
            {
                pos += ray.direction * 0.01f;
            }
            else
            {
                pos -= ray.direction * 0.01f;
            }

            int posX = Mathf.RoundToInt(pos.x);
            int posY = Mathf.RoundToInt(pos.y);
            int posZ = Mathf.RoundToInt(pos.z);

            selectBrushTransform.transform.position = new Vector3(posX, posY, posZ);
            if (inside)
            {
                switch (mastermind.brushDirection)
                {
                    case VoxelDirection.Right:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(eposelong + 1, 0.5f, 0.5f);
                        directionMark.transform.eulerAngles = new Vector3(0, 270, 0);
                        break;
                    case VoxelDirection.Left:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(-eposelong, 0.5f, 0.5f);
                        directionMark.transform.eulerAngles = new Vector3(0, 90, 0);
                        break;
                    case VoxelDirection.Up:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(0.5f, eposelong + 1, 0.5f);
                        directionMark.transform.eulerAngles = new Vector3(90, 0, 0);
                        break;
                    case VoxelDirection.Down:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(0.5f, -eposelong, 0.5f);
                        directionMark.transform.eulerAngles = new Vector3(270, 0, 0);
                        break;
                    case VoxelDirection.Front:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(0.5f, 0.5f, eposelong + 1);
                        directionMark.transform.eulerAngles = new Vector3(0, 180, 0);
                        break;
                    case VoxelDirection.Break:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(0.5f, 0.5f, -eposelong );
                        directionMark.transform.eulerAngles = new Vector3(0, 0, 0);
                        break;
                    default:
                        break;
                }

            }
            else
            {

                switch (mastermind.brushDirection)
                {
                    case VoxelDirection.Right:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(eposelong, 0.5f, 0.5f);
                        directionMark.transform.eulerAngles = new Vector3(0, 270, 0);
                        break;
                    case VoxelDirection.Left:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(-eposelong + 1, 0.5f, 0.5f);
                        directionMark.transform.eulerAngles = new Vector3(0, 90, 0);
                        break;
                    case VoxelDirection.Up:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(0.5f, eposelong, 0.5f);
                        directionMark.transform.eulerAngles = new Vector3(90, 0, 0);
                        break;
                    case VoxelDirection.Down:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(0.5f, -eposelong + 1, 0.5f);
                        directionMark.transform.eulerAngles = new Vector3(270, 0, 0);
                        break;
                    case VoxelDirection.Front:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(0.5f, 0.5f, eposelong);
                        directionMark.transform.eulerAngles = new Vector3(0, 180, 0);
                        break;
                    case VoxelDirection.Break:
                        directionMark.transform.position = selectBrushTransform.transform.position + new Vector3(0.5f, 0.5f, -eposelong + 1);
                        directionMark.transform.eulerAngles = new Vector3(0, 0, 0);
                        break;
                    default:
                        break;
                }
            }

        }
        else
        {
            return false;
        }

        return true;
    }
}
